package com.louis.ylog;

import net.logstash.logback.marker.LogstashMarker;
import net.logstash.logback.marker.MapEntriesAppendingMarker;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ELKTagBuilder {

    /**
     * 标签
     */
    private final  Map<String,String> tags;

    private ELKTagBuilder(){
        this(new ConcurrentHashMap<>());
    }

    private ELKTagBuilder(Map<String,String> tags){
        this.tags= new ConcurrentHashMap<>(tags);
    }

    /**
     * 数据库主键
     * @param id
     * @return
     */
    public ELKTagBuilder id(Number id){
        return putCustomerField("Id",String.valueOf(id));
    }

    /**
     * 标题
     * @param title
     * @return
     */
    public ELKTagBuilder title(String title){
        return putCustomerField("Title",String.valueOf(title));
    }

    /**
     * 前端请求唯一id
     * @param requestId
     * @return
     */
    public ELKTagBuilder requestId(String requestId){
        return putCustomerField("Id",String.valueOf(requestId));
    }

    public ELKTagBuilder saveInTiger(){
        return putCustomerField("__CustomerESIndex","true").putCustomerField("__CustomerESIndexName","_tiger");
    }

    public ELKTagBuilder putCustomerField(String key, String value){
        if(StringUtils.isEmpty(key)){
            key="N/A";
        }
        if(StringUtils.isEmpty(value)){
            value="N/A";
        }
        tags.put(key,value);
        return this;
    }


    public static ELKTagBuilder create(Number id){
        return create().id(id);
    }

    public static ELKTagBuilder create(String title){
        return create().title(title);
    }



    public static ELKTagBuilder create(Map<String,String> tags){
        return new ELKTagBuilder(tags);
    }
    public static ELKTagBuilder create(){
        return new ELKTagBuilder();
    }

    public LogstashMarker build(){
        //Marker
        return new MapEntriesAppendingMarker(tags);
    }
}
